package bairui.international.console.web.controller.index.module;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.console.web.controller.product.product.C_Product_Product_Show_ShowImg;
import bairui.international.model.domain.jpa.TB_IndexModuleProduct;
import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.service.Service_IndexModuleProduct;

/**
 *
 * 详情
 *
 * @generated
 * 
 * @see STest_C_Index_Module_Detail_ProductPage 测试
 *
 */
@Controller
public class C_Index_Module_Detail_ProductPage
 extends SC_Index_Module_Detail_ProductPage 
{
	
	@Autowired private Service_IndexModuleProduct service_IndexModuleProduct;
	
	/**
	 * 产品列表
	 */
	@SuppressWarnings("serial")
	@Action(ProductPage.class)
	@GetMapping(value=ProductPage.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object productPage(
		HttpServletRequest request, 
		@PathVariable(name=ProductPage.PathVariableName.Id) long id
	) throws Exception {
		org.springframework.data.domain.Page<TB_IndexModuleProduct> page = service_IndexModuleProduct.page(
				it->ProductPage.Filter.query()
					.and(it.indexModule.id.eq(id)), 
				ProductPage.Output.page(request), 
				it->ProductPage.Output.sort(it, request, new ProductPage.Output.OrderExtends() {
					@Override public Sort gainPrice(Direction direction) {
						return sort(direction, it.product.price);
					}
					@Override public Sort gainNumber(Direction direction) {
						return sort(direction, it.product.number);
					}
					@Override public Sort gainName(Direction direction) {
						return sort(direction, it.product.name);
					}
					@Override public Sort gainImgUrl(Direction direction) {
						return sort(direction, it.product.imgUrl);
					}
				})
			);
		//
		return ProductPage.Output.output(
				page
					.map(it->new ProductPage.Output.Model(it){
							private TB_Product product;
							@Override protected void assign_before() {
								product = it.getProduct();
							}
							@Override public String assignImgUrl() {
								return new C_Product_Product_Show_ShowImg.ShowImg.Operate(){
									@Override public Object getId() {
										return product.getId();
									}
								}.getUrl();
							}
							@Override public String assignNumber() {
								return product.getNumber();
							}
							@Override public String assignName() {
								return product.getName();
							}
							@Override public Double assignPrice() {
								return product.getPrice();
							}
						}
					)
			);
	}
	

}
